Ext.define("Triends.Model.PostLocationComment",{
	extend: "Ext.data.Model",
	config: {
		fields: [
			"Number", "PostNumber", "WriterID", "Writer", "Text", "Date", "Time"
		]
	}
});

var PostLocationCommentStore = Ext.create("Ext.data.Store",{
	model: "Triends.Model.PostLocationComment",
	id: 'PostLocationCommentStore',
	proxy: {
		type: "jsonp",
		callbackKey: 'callback',
		url: "http://itscreater.cafe24.com/Triends/PostCommentList.jsp"
	},
	sorters: [
		{
			property: 'Date',
			direction: 'ASC'
		},
		{
			property: 'Time',
			direction: 'ASC'
		}
	]
});

Ext.define('Triends.view.PostLocationComment', {
	extend: 'Ext.dataview.DataView',
	xtype: 'postlocationcomment',
	config: {
 		minHeight: 100,
 		style: 'margin-top:10px;',
 		width:'100%',
 		scrollable: false,
 		styleHtmlContent: true, // 이게 있으면 CSS가 제대로 안먹을수도, 없으면 하이퍼링크 안걸림
 		store: PostLocationCommentStore,
	   	useComponents: true,
		defaultType: 'PostLocationCommentItem',
	   	items: [{
    		xtype: 'panel',
    		docked: 'bottom',
    		layout: 'hbox',
    		style: "padding-right:15px",
    		items: [{
    			xtype: 'textfield',
    			id: 'postlocationcommentText',
   				flex: 9,
   				style: 'height: 30px; font-size: 70%; padding: 0px; margin-top:3px;'
    		},
    		{
    			xtype: 'button',
    			iconCls: 'compose',
    			width: 50,
    			iconMask: true,
    			flex: 1,
    			handler: function() {
    				var thisview = Ext.getCmp('postlocationtitlepanel1');
	        		var postnum = thisview.getData().Number;
		    		var userid = Ext.getStore('UserStore').getAt(0).data.ID;
		    		var username = Ext.getStore('UserStore').getAt(0).data.Name;
		    		var text = Ext.getCmp('postlocationcommentText').getValue();
		    		
		    		Ext.Ajax.request({
 						url: 'http://itscreater.cafe24.com/Triends/PostCommentWrite.jsp',
 						params: {
 							id: userid,
 							name: username,
 							num: postnum, 
 							text: text
 						},
 						success: function(response) {
 							var PostLocationCommentStore = Ext.getStore('PostLocationCommentStore');
 							PostLocationCommentStore.getProxy().setExtraParams({num: postnum});
 							PostLocationCommentStore.load({
		 						callback: function() {
 									Ext.getCmp('postlocationcommentText').reset();
 									Ext.Msg.alert('', '댓글이 등록되었습니다.', Ext.emptyFn);
 								}
							}); 			
 						}
 					});
 				}
    		}]
   		}]
	}
});

Ext.define('PostLocationCommentItem', {
	extend: 'Ext.dataview.component.DataItem',
    xtype: 'PostLocationCommentItem',
	config: {
		style: 'margin-left:-15px;',
		listeners: {
			initialize: function() {
				var writerid = this.getRecord().get('WriterID');
				
				var img = this.getItems().items[1];
				img.setSrc('https://graph.facebook.com/'+writerid+'/picture');
				
				var userid = UserStore.getAt(0).data.ID;
				var delbtn = this.getItems().items[2];
				if (userid == writerid) {
					delbtn.show();
				} else {
					Ext.emptyFn
				}
			}
		},
		layout: 'hbox',
		width: '105%',
		style: 'background:white;border-bottom:1px #CBCAC7 solid;margin-left:-17px',
		items: [
		{
			xtype: 'panel',
			style: 'word-break:break-all; padding-right:35px',
			tpl: '<a class="writer">{Writer}</a><br>{Text}<br><font size="2">{Date}&nbsp;&nbsp;&nbsp;&nbsp;{Time}</font>',
			listeners: {
				tap: {
					element: 'element',
					delegate: 'a.writer',
					fn: function() {
						var dataitem = this.getParent();
						var otherid = dataitem.getRecord().get('WriterID');
						openOtherinfo(otherid);
					}
				}
			}
		},
		{
			xtype: 'img',
			height: 64,
			width: 64,
			docked: 'left',
			listeners: {
				tap: function() {
					var dataitem = this.getParent();
					var otherid = dataitem.getRecord().get('WriterID');
					openOtherinfo(otherid)
				}
			}
		},
		{
			xtype: 'button',
			text: 'X',
			style: 'background:white;border:0px solid',
			width: 32,
			height: 32,
			docked: 'right',
			hidden: true,
			handler: function() {
				var dataitem = this.getParent();
				var commentnum = dataitem.getRecord().get('Number');
				var postnum = dataitem.getRecord().get('PostNumber');
				
				Ext.Msg.show({
  					title: '알림',
  					message: '삭제하시겠습니까?',
   		        	width: 300,
   					buttons:  [{
   						id: 'ok',
   						text: 'Ok',
   						ui: 'action'
   					},
   					{	
   						id: 'cancel',
   						text: 'Cancel'
   					}],   					
   					fn: function(buttonId) {
       					if (buttonId == 'Ok') {
       						Ext.data.JsonP.request({
 								url: 'http://itscreater.cafe24.com/Triends/PostCommentDelete.jsp',
 								params: {num: commentnum},
 								callbackkey: 'callback',
 								success: function() {
 									var PostLocationCommentStore = Ext.getStore('PostLocationCommentStore');
 									PostLocationCommentStore.getProxy().setExtraParams({num: postnum});
 									PostLocationCommentStore.load(); 			
 								}
 							});
						} else if (buttonId == 'Cancel') {
       						Ext.emptyFn
       					}
   					}
				});
			}
		}]
    }
});